//
//  GradientLabelView.swift
//  STest
//
//  Created by wang tiao on 2025/9/13.
//


import UIKit

class GradientTextLabel: UIView {
    
    private let label = UILabel()
    private let gradientLayer = CAGradientLayer()
    
    /// 初始化
    init(text: String = "努力制作中...") {
        super.init(frame: .zero)
        setupUI(text: text)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI(text: "努力制作中...")
    }
    
    private func setupUI(text: String) {
        // 设置 label
        label.text = text
        label.font = UIFont.systemFont(ofSize: 28, weight: .bold)
        label.textAlignment = .center
        label.textColor = .white // 占位色，实际用渐变覆盖
        label.sizeToFit()
        addSubview(label)
        
        // 渐变图层
        gradientLayer.colors = [
            UIColor.systemBlue.cgColor,
            UIColor.systemPurple.cgColor,
            UIColor.systemPink.cgColor
        ]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
        layer.addSublayer(gradientLayer)
        
        // 用 gradientLayer 的 mask 实现文字渐变
        gradientLayer.mask = label.layer
        label.frame = bounds
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
        label.frame = bounds
    }
}


//
//import UIKit
//
//class GradientLabelView: UIView {
//    
//    private let label = UILabel()
//    private let gradientLayer = CAGradientLayer()
//    
//    /// 渐变颜色数组
//    var gradientColors: [UIColor] = [.systemBlue, .systemPurple, .systemPink] {
//        didSet {
//            gradientLayer.colors = gradientColors.map { $0.cgColor }
//        }
//    }
//    
//    /// 动画时长
//    var animationDuration: CFTimeInterval = 3.0
//    
//    /// 文本内容
//    var text: String? {
//        didSet {
//            label.text = text
//            invalidateIntrinsicContentSize()
//            setNeedsLayout()
//        }
//    }
//    
//    /// 字体
//    var font: UIFont = UIFont.systemFont(ofSize: 28, weight: .bold) {
//        didSet {
//            label.font = font
//            invalidateIntrinsicContentSize()
//            setNeedsLayout()
//        }
//    }
//    
//    // MARK: - 初始化
//    init(text: String = "努力制作中...") {
//        super.init(frame: .zero)
//        self.text = text
//        setupUI()
//    }
//    
//    required init?(coder: NSCoder) {
//        super.init(coder: coder)
//        self.text = "努力制作中..."
//        setupUI()
//    }
//    
//    private func setupUI() {
//        // Label
//        label.text = text
//        label.font = font
//        label.textAlignment = .center
//        label.textColor = .white
//        addSubview(label)
//        
//        // Gradient
//        gradientLayer.colors = gradientColors.map { $0.cgColor }
//        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
//        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
//        layer.addSublayer(gradientLayer)
//        gradientLayer.mask = label.layer
//    }
//    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        gradientLayer.frame = bounds
//        label.frame = bounds
//    }
//    
//    override var intrinsicContentSize: CGSize {
//        return label.intrinsicContentSize
//    }
//    
//    // MARK: - 动画
//    func startAnimating() {
//        let animation = CABasicAnimation(keyPath: "locations")
//        animation.fromValue = [-0.3, 0.0, 0.3]
//        animation.toValue = [0.7, 1.0, 1.3]
//        animation.duration = animationDuration
//        animation.repeatCount = .infinity
//        animation.isRemovedOnCompletion = false
//        gradientLayer.locations = [-0.3, 0.0, 0.3]
//        gradientLayer.add(animation, forKey: "gradientFlow")
//    }
//    
//    func stopAnimating() {
//        gradientLayer.removeAnimation(forKey: "gradientFlow")
//    }
//}


import UIKit

class GradientLabelView: UIView {
    
    private let label = UILabel()
    private let gradientLayer = CAGradientLayer()
    
    /// 渐变颜色
    var gradientColors: [UIColor] = [.systemBlue, .systemPurple, .systemPink] {
        didSet {
            gradientLayer.colors = gradientColors.map { $0.cgColor }
        }
    }
    
    /// 动画时长
    var animationDuration: CFTimeInterval = 2.5
    
    /// 文本
    var text: String? {
        didSet {
            label.text = text
            invalidateIntrinsicContentSize()
            setNeedsLayout()
        }
    }
    
    /// 字体
    var font: UIFont = UIFont.systemFont(ofSize: 28, weight: .bold) {
        didSet {
            label.font = font
            invalidateIntrinsicContentSize()
            setNeedsLayout()
        }
    }
    
    // MARK: - 初始化
    init(text: String = "努力制作中...") {
        super.init(frame: .zero)
        self.text = text
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.text = "努力制作中..."
        setupUI()
    }
    
    private func setupUI() {
        // Label
        label.text = text
        label.font = font
        label.textAlignment = .center
        label.textColor = .white
        addSubview(label)
        
        // Gradient
        gradientLayer.colors = gradientColors.map { $0.cgColor }
        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
        layer.addSublayer(gradientLayer)
        gradientLayer.mask = label.layer
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
        label.frame = bounds
    }
    
    override var intrinsicContentSize: CGSize {
        return label.intrinsicContentSize
    }
    
    // MARK: - 动画（左右流动）
    func startAnimating() {
        let animation = CABasicAnimation(keyPath: "locations")
        animation.fromValue = [-0.3, 0.0, 0.3]
        animation.toValue = [0.7, 1.0, 1.3]
        animation.duration = animationDuration
        animation.autoreverses = true // 关键：来回动
        animation.repeatCount = .infinity
        animation.isRemovedOnCompletion = false
        gradientLayer.locations = [-0.3, 0.0, 0.3]
        gradientLayer.add(animation, forKey: "gradientLeftRight")
    }
    
    func stopAnimating() {
        gradientLayer.removeAnimation(forKey: "gradientLeftRight")
    }
}

//import UIKit
//
//class GradientLabelView: UIView {
//    
//    private let label = UILabel()
//    private let gradientLayer = CAGradientLayer()
//    
//    /// 渐变颜色
//    var gradientColors: [UIColor] = [.systemBlue, .systemPurple, .systemPink] {
//        didSet {
//            gradientLayer.colors = gradientColors.map { $0.cgColor }
//        }
//    }
//    
//    /// 动画时长
//    var animationDuration: CFTimeInterval = 3.0
//    
//    /// 文本
//    var text: String? {
//        didSet {
//            label.text = text
//            invalidateIntrinsicContentSize()
//            setNeedsLayout()
//        }
//    }
//    
//    /// 字体
//    var font: UIFont = UIFont.systemFont(ofSize: 28, weight: .bold) {
//        didSet {
//            label.font = font
//            invalidateIntrinsicContentSize()
//            setNeedsLayout()
//        }
//    }
//    
//    // MARK: - 初始化
//    init(text: String = "努力制作中...") {
//        super.init(frame: .zero)
//        self.text = text
//        setupUI()
//    }
//    
//    required init?(coder: NSCoder) {
//        super.init(coder: coder)
//        self.text = "努力制作中..."
//        setupUI()
//    }
//    
//    private func setupUI() {
//        // Label
//        label.text = text
//        label.font = font
//        label.textAlignment = .center
//        label.textColor = .white
//        addSubview(label)
//        
//        // Gradient
//        gradientLayer.colors = gradientColors.map { $0.cgColor }
//        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
//        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
//        layer.addSublayer(gradientLayer)
//        gradientLayer.mask = label.layer
//    }
//    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        gradientLayer.frame = bounds
//        label.frame = bounds
//    }
//    
//    override var intrinsicContentSize: CGSize {
//        return label.intrinsicContentSize
//    }
//    
//    // MARK: - 动画（更顺滑的左右流动）
//    func startAnimating() {
//        let animation = CABasicAnimation(keyPath: "locations")
//        animation.fromValue = [-1.0, -0.5, 0.0]
//        animation.toValue = [1.0, 1.5, 2.0]
//        animation.duration = animationDuration
//        animation.autoreverses = true
//        animation.repeatCount = .infinity
//        animation.isRemovedOnCompletion = false
//        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
//        
//        gradientLayer.locations = [-1.0, -0.5, 0.0]
//        gradientLayer.add(animation, forKey: "smoothLeftRight")
//    }
//    
//    func stopAnimating() {
//        gradientLayer.removeAnimation(forKey: "smoothLeftRight")
//    }
//}
